package com.gem.web;

import com.gem.entity.Commodity;
import com.gem.entity.Customer;
import com.gem.entity.Order;
import com.gem.entity.OrderDetails;
import com.gem.service.CommodityService;
import com.gem.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/permission/order")
@SessionAttributes(value = {"cart"})
public class OrderController {

    @Autowired
    CommodityService commodityService;
    @Autowired
    OrderService orderService;

    /**
     * 下单方法
     * @param model
     * @param id
     * @param num
     * @return
     */
    @ResponseBody
    @RequestMapping("buy")
    public String buy(Model model,int id,int num,HttpSession session){
        Customer customer = (Customer) session.getAttribute("customer");
        //Customer customer = (Customer) model.getAttribute("customer");
        Commodity commodity = commodityService.findCommodityById(id);

        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setNum(num);
        orderDetails.setPrice(commodity.getPrice());
        orderDetails.setCommodity(commodity);
        orderDetails.setSubtotal(commodity.getPrice() * num);
        List<OrderDetails> list = new ArrayList<>();
        list.add(orderDetails);

        Order order = new Order();
        order.setCustomer(customer);
        order.setOrderId(UUID.randomUUID().toString().replace("-",""));
        order.setOrderDetails(list);
        order.setCreateTime(LocalDateTime.now());

        orderDetails.setOrder(order);

        orderService.addOrder(order,list);
        return "1";
    }


    /**
     * 跳转到购买成功页面
     * @return
     */
    @RequestMapping("/toSuccess")
    public String toSuccess(){
        return "successBuy";
    }

    /**
     * 展示我的订单
     * @param model
     * @return
     */
    @RequestMapping("/showMyOrders")
    public String showMyOrders(Model model, HttpSession session){
        Customer customer = (Customer) session.getAttribute("customer");
        List<Order> orders = orderService.findOrderByCustomerId(customer.getId());
        List<OrderDetails> orderDetailsList = new ArrayList<>();
        if (orders != null) {
            for (Order order : orders) {
                List<OrderDetails> orderDetails = order.getOrderDetails();
                for (OrderDetails o : orderDetails) {
                    Commodity commodity = commodityService.findCommodityById(o.getCommodity().getId());
                    Order order1 = orderService.findOrderById(o.getOrder().getId());
                    o.setCommodity(commodity);
                    o.setOrder(order1);
                    orderDetailsList.add(o);
                }
            }
        }
        model.addAttribute("orderDetailsList",orderDetailsList);
        return "myOrder";
    }

    @ResponseBody
    @RequestMapping("/settlement")
    public String settlement(Model model, HttpServletRequest request,SessionStatus sessionStatus){
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");


        List<OrderDetails> orderDetailsList = (List<OrderDetails>) model.asMap().get("cart");
        Order order = new Order();
        order.setCreateTime(LocalDateTime.now());
        order.setCustomer(customer);
        order.setOrderDetails(orderDetailsList);
        order.setOrderId(UUID.randomUUID().toString().replace("-",""));


        for (OrderDetails orderDetails : orderDetailsList) {
            orderDetails.setOrder(order);
        }

        orderService.addOrder(order,orderDetailsList);

        //移除session里的cart信息
        //model.addAttribute("cart",null);
        sessionStatus.setComplete();
        return "1";
    }


}
